package controllers;

import java.util.List;

import models.Handshake;
import models.Notification;
import models.Offer;
import models.Request;
import models.Handshake.Status;
import models.User;

public class Application extends BaseController
{   
    public static void index()
	{
	
	int recentHandshakeCount = 5;
	int userStatus = 0;
	int userDifference = 0;
	List<Handshake> newHandshakesList = Handshake.find("(status='ACCEPTED' or status='STARTED' or status='DONE') order by creationDate desc").fetch(recentHandshakeCount);
	
	
	
	/*
	We are going to try to list our current notification(s) if exist(s)
	*/
	User user = getConnectedUser();
	
	int totalNumberOfNotifications = 0;
	
	List<Notification> newHandshakes;
	List<Notification> newOffers;
	List<Notification> newRequests;
	List<Notification> newComments;
	
	if( user != null )
	{
	
	
	//We are getting servicesGiven and servicesTaken information of the user
	int servicesGiven = user.servicesGiven;
	int servicesTaken = user.servicesTaken;
	
	/*
	There are three possible statuses for the user
	*/
	if( servicesGiven > servicesTaken )
	{
		//User should take more services to make difference smaller
		userStatus = 1;
		userDifference = servicesGiven - servicesTaken;
		
	}
	else if( servicesGiven < servicesTaken )
	{
		//User should give more services to make difference smaller
		userStatus = 2;
		userDifference = servicesTaken - servicesGiven;
	}
	else
	{
		//User is cool :)
		userDifference = 0;
		userStatus = 3;
	}
	
	if( userDifference > 10 )
	{
		//This user may be leecher !
		userStatus = userStatus + 10;
	}
	
	
	newHandshakes = 
	Notification.find("status='SHOW' AND type='Handshakee' AND user.id=" + 
	user.id +
	" AND ( handshake.status='ACCEPTED' OR handshake.status='IGNORED' OR handshake.status='REJECTED' OR handshake.status='CANCELLED'  OR handshake.status='NOTVOTED')").fetch(5);
	
	newOffers = Notification.find("status='SHOW' AND type='Offerr' AND user.id=" + user.id + " AND handshake.status='WAITING_APPROVAL'").fetch(5);
	
	newRequests = Notification.find("status='SHOW' AND type='Requestt' AND user.id=" + user.id + " AND handshake.status='WAITING_APPROVAL'").fetch(5);
	
	newComments = Notification.find("status='SHOW' AND type='Commentt' AND user.id=" + user.id).fetch(5);
	
	totalNumberOfNotifications = newHandshakes.size() + newOffers.size() + newRequests.size() + newComments.size();
	}
	else
	{
	newHandshakes = null;
	newOffers = null;
	newRequests = null;
	newComments = null;
	}
	
	render( newHandshakes, newOffers, newRequests, newComments, totalNumberOfNotifications, newHandshakesList, userDifference, userStatus );
	
	}

    public static void register() {
	render();
    }

    public static void about() {
	render();
    }

    public static void faq() {
	render();
    }

    public static void contact() {
	render();
    }
    
    public static void termsOfService() {
	render();
    }

    public static void showUserPhoto(Long userId) {
	User user = User.findById(userId);
	renderBinary(user.photo.get());
    }
	
	public static void notification()
	{
	
	/*
	We are going to try to list our current notification(s) if exist(s)
	*/
	User user = getConnectedUser();
	

	
	List<Notification> newHandshakes;
	List<Notification> newOffers;
	List<Notification> newRequests;
	List<Notification> newComments;
	
	if( user != null )
	{
	newHandshakes = 
	Notification.find("type='Handshakee' AND user.id=" + 
	user.id 
	+ " AND ( handshake.status='ACCEPTED' OR handshake.status='IGNORED' OR handshake.status='REJECTED' OR handshake.status='CANCELLED'  OR handshake.status='NOTVOTED' OR handshake.status='DONE')").fetch();
	
	newOffers = Notification.find("type='Offerr' AND user.id=" + user.id).fetch();
	
	newRequests = Notification.find("type='Requestt' AND user.id=" + user.id).fetch();
	
	newComments = Notification.find("type='Commentt' AND user.id=" + user.id).fetch();
	

	}
	else
	{
	newHandshakes = null;
	newOffers = null;
	newRequests = null;
	newComments = null;
	}
	
	render( newHandshakes, newOffers, newRequests, newComments );
	
	}

}
